package com.jdrx.sw.sewage.service;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.AlarmSetDTO;
import com.jdrx.sw.sewage.beans.entity.AlarmSetPO;
import com.jdrx.sw.sewage.beans.vo.AlarmSetVO;
import com.jdrx.sw.sewage.common.enums.EAlarmLevel;
import com.jdrx.sw.sewage.common.enums.EAlarmSetEffectType;
import com.jdrx.sw.sewage.common.util.Asserts;
import com.jdrx.sw.sewage.common.util.BeanUtils;
import com.jdrx.sw.sewage.dao.AlarmSetDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AlarmSetServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(AlarmSetServiceImpl.class);

    @Autowired
    private AlarmSetDAO alarmSetDAO;

    public List<AlarmSetVO> selectList() {
        return alarmSetDAO.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseVO updateYellowAlarmSetById(AlarmSetDTO alarmSetDTO) throws BizException {
        if (null == alarmSetDTO.getId()) return ResponseFactory2.err("告警设置id不能为空");
        Asserts.notNull(alarmSetDTO.getId(), "");

        AlarmSetPO alarmSetPO = alarmSetDAO.selectByPrimaryKey(alarmSetDTO.getId());
        if (null == alarmSetPO) return ResponseFactory2.err("该设备id不存在");

        if (EAlarmLevel.ALARM_RED.getKey() == alarmSetPO.getAlarmLevel())
            return ResponseFactory2.err("红色告警设置不能修改");

        //ph值被红色告警包含才能修改ph
        AlarmSetPO redAlarmSetPO = alarmSetDAO.findByLevelAndTffect(EAlarmLevel.ALARM_RED.getKey(), EAlarmSetEffectType.EFFECT.getKey(), null);
        if (redAlarmSetPO.getPhMax() <= alarmSetDTO.getPhMax() || redAlarmSetPO.getPhMin() >= alarmSetDTO.getPhMin())
            return ResponseFactory2.err("自定义ph值范围只能在标准范围区间内");

        BeanUtils.copyProperties(alarmSetDTO, alarmSetPO);
        try {
            alarmSetDAO.updateByPrimaryKey(alarmSetPO);
        } catch (Exception e) {
            logger.error("更新告警设置失败", e);
            throw new BizException("更新告警设置失败");
        }
        return ResponseFactory2.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseVO updateRedAlarmSetById(Long alarmSetId) throws BizException {
        AlarmSetPO alarmSetPO = alarmSetDAO.selectByPrimaryKey(alarmSetId);
        if (null == alarmSetPO) return ResponseFactory2.err("该设备id不存在");

        alarmSetPO.setTakeEffect(EAlarmSetEffectType.EFFECT.getKey());
        if (EAlarmLevel.ALARM_RED.getKey() == alarmSetPO.getAlarmLevel()) {
            AlarmSetPO redAlarmSetPO = alarmSetDAO.findByLevelAndTffect(EAlarmLevel.ALARM_RED.getKey(), EAlarmSetEffectType.EFFECT.getKey(), null);
            redAlarmSetPO.setTakeEffect(EAlarmSetEffectType.NO_EFFECT.getKey());
            alarmSetDAO.updateByPrimaryKey(redAlarmSetPO);
            alarmSetDAO.updateByPrimaryKey(alarmSetPO);
        }

        if (EAlarmLevel.ALARM_YELLOW.getKey() == alarmSetPO.getAlarmLevel()) alarmSetDAO.updateByPrimaryKey(alarmSetPO);

        return ResponseFactory2.ok();
    }
}